package com.yuanxiao.common.core.utils;

import org.apache.commons.lang3.EnumUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @author: luozongsheng
 * @mail: lzs1115@foxmail.com
 * @date: 2021-10-19 14:14
 **/
public class MyEnumUtils {


    private static final transient Logger logger = LoggerFactory.getLogger(MyEnumUtils.class);

    /**
     * 判断某个枚举是否包某个code值
     * @param enumClass 需要判断是否存在那个枚举类中
     * @param value 需要判断的值
     * @return 包含返回true，否则返回false
     */
    public static boolean isInclude(Class enumClass, String value){
        List enumList = EnumUtils.getEnumList(enumClass);
        for (int i = 0;i<enumList.size(); i++){
            Object en = enumList.get(i);
            Class<?> enClass = en.getClass();
            try {
                Method method = enClass.getMethod("getValue"); // 需要与枚举类方法对应
                Object invoke = method.invoke(en, null);
                if(invoke.toString().equals (value)) {
                    return true;
                }
            }catch (Exception e){
                logger.error("枚举执行getValue方法失败...");
            }
        }
        return false;
    }

}
